package com.kingbook.api.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingbook.common.utils.DateUtils;
import com.kingbook.core.annotations.ContainProperties;
import com.kingbook.core.annotations.PropertieVerify;
import com.kingbook.core.auth.LoginManager;
import com.kingbook.core.result.JSONResult;
import com.kingbook.entity.Driver;
import com.kingbook.entity.Vehicle;
import com.kingbook.services.CategoryService;
import com.kingbook.services.DriverService;
import com.kingbook.services.TonsService;
import com.kingbook.services.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.DriverManager;
import java.util.Map;

import static com.kingbook.common.enums.DateFormatEnum.DATE;
import static com.kingbook.core.auth.LoginManager.AUTH;

/**
 * @Author Bucky
 * @Date 2020-01-08 21:13:15
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("vehicle")
public class VehicleRest {

    @Autowired
    VehicleService vehicleService;

    @PutMapping
    @ContainProperties(properties = {@PropertieVerify(name = "id"),
            @PropertieVerify(name = "brand", maxLength = 20),
            @PropertieVerify(name = "model", maxLength = 10),
            @PropertieVerify(name = "plateNo", maxLength = 7),
            @PropertieVerify(name = "tons", maxLength = 4, maxValue = 2000),
            @PropertieVerify(name = "amount", maxLength = 6),
            @PropertieVerify(name = "driver", maxLength = 5)}
    )
    public JSONResult save(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        log.info("---->{}", param);
        return vehicleService.updateVehicle(param);
    }

    @DeleteMapping("{id}")
    public JSONResult remove(@PathVariable int id, @RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        log.info("---->mid={} id={}", mid, id);
        return vehicleService.remove(id, mid);
    }

    @GetMapping("{id}")
    public JSONResult find(@PathVariable int id, @RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        log.info("---->mid={} id={}", mid, id);
        JSONResult r = vehicleService.find(id, mid);
        if (r.isSuccess() && r.getCount() > 0) {
            JSONObject j = r.getData().getJSONObject(0);
            j.put("productionDate", DateUtils.format(j.getString("productionDate"), DATE));
            j.put("nextAuditDate", DateUtils.format(j.getString("nextAuditDate"), DATE));
            j.put("insuranceEnd", DateUtils.format(j.getString("insuranceEnd"), DATE));
        }
        return r;
    }

    @PostMapping("query")
    public JSONResult query(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        log.info("---->{}", param);
        JSONResult result = vehicleService.query(param);
        JSONArray array = result.getData();
        JSONArray newArray = new JSONArray();
        for (int i = 0; i < array.size(); i++) {
            JSONObject order = array.getJSONObject(i);
            JSONObject json = new JSONObject();
            json.put("id", order.getIntValue("id"));
            json.put("avatarUrl", order.getString("image"));
            json.put("title", order.getString("plateNo"));
            json.put("subTitle", "驾驶员 " + order.getString("driver"));
            Integer jib = order.getInteger("jib");
            json.put("lable", order.getString("tons") + "T" + (jib != null && jib > 0 ? "-" + jib : ""));
            json.put("desc1", "品牌型号: " + order.getString("brand") + "  --  " + order.getString("model"));
            json.put("desc2", "出厂时间: " + order.getString("productionDate"));
            json.put("desc3", "年审到期: " + order.getString("nextAuditDate"));
            json.put("desc4", "保险到期: " + order.getString("insuranceEnd"));
            json.put("desc5", "保险公司: " + order.getString("insuranceCompany"));
            int orderCount = order.getIntValue("orderCount");
            json.put("workspaceCount", 0);
            json.put("orderCount", orderCount);
            json.put("del", orderCount == 0);
            newArray.add(json);
        }
        result.setData(newArray);
        return result;
    }

    @PostMapping("pages")
    @ContainProperties({"pageNo", "pageSize"})
    public JSONResult queryForPages(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        log.info("---->{}", param);
        return vehicleService.queryForPages(param);
    }

}
